package com.asiainfo.utils;

import com.sun.istack.internal.NotNull;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

import java.security.SecureRandom;
import java.util.HashMap;

/**
 * JdbcHelper
 *
 * @Author jhr
 * @Date 2022/3/21
 */
public class JdbcHelper {
    static JdbcTemplate jdbcTemplate= null;
    public static HashMap<String, JdbcTemplate> templateMap = new HashMap<>();
    public static JdbcTemplate createMysqlTemplate(String templateName,String url,String username,String password,int initialSize,int maxActive){
//        DBCP数据库连接池
        BasicDataSource dataSource = new BasicDataSource();
//        mysql 8.0
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
//      url: jdbc:mysql://localhost:3306/qing?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setInitialSize(initialSize);
        dataSource.setMaxActive(maxActive);
        JdbcTemplate template = new JdbcTemplate(dataSource);
        templateMap.put(templateName,template);
        return template;
    }
    public static JdbcTemplate getJdbcTemplate(String templateName){
        return templateMap.get(templateName);
    }

    public static void main(String[] args) {
        jdbcTemplate = createMysqlTemplate("mysql","jdbc:mysql://localhost:3306/qing?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true","root","password",5,30);
//        查询语句，返回string类型数据
        String version = jdbcTemplate.queryForObject("select VERSION()", String.class);
        System.out.println("本机的MySQL版本："+version);
    }
}
